package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.enums.TocSaleImportLogOrderStatusEnum;
import com.ethink.order.api.info.TocSaleImportLogDetailInfo;
import com.ethink.order.api.info.TocSaleImportLogInfo;
import com.ethink.order.api.model.TocSaleImportLogModel;
import com.ethink.order.api.request.CheckShopOrderNoIfExistRequest;
import com.ethink.order.api.request.SaleOrderUpdateStatusRequest;
import com.ethink.order.api.request.TocSaleImportLogAddRequest;
import com.ethink.order.api.request.TocSaleImportLogQueryRequest;
import com.ethink.order.api.response.TocSaleImportLogQueryDetailResponse;
import com.ethink.order.common.entity.SaleOrder;
import com.ethink.order.common.entity.TocSaleImportLog;
import com.ethink.order.common.entity.TocSaleImportLogDetail;
import com.ethink.order.common.mapper.TocSaleImportLogDetailMapper;
import com.ethink.order.common.mapper.TocSaleImportLogMapper;
import com.ethink.order.converter.TocSaleImportLogConverter;
import com.ethink.order.manager.SaleChannelRequestManager;
import com.ethink.order.service.TocSaleImportLogDetailService;
import com.ethink.order.service.TocSaleImportLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TOC销售单导入日志 Service接口实现
 *
 * @author default
 * @date 2022-11-22
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TocSaleImportLogServiceImpl implements TocSaleImportLogService {

    @Autowired
    private TocSaleImportLogMapper tocSaleImportLogMapper;

    @Autowired
    private TocSaleImportLogConverter tocSaleImportLogConverter;

    @Autowired
    private TocSaleImportLogDetailService tocSaleImportLogDetailService;

    @Autowired
    private TocSaleImportLogDetailMapper tocSaleImportLogDetailMapper;

    @Override
    public Integer addTocSaleOrder(TocSaleImportLogAddRequest request) {
        TocSaleImportLog tocSaleImportLog = BeanUtil.copyProperties(request, TocSaleImportLog.class, "item");
        int insert = tocSaleImportLogMapper.insert(tocSaleImportLog);
        tocSaleImportLogDetailService.batchInsert(request.getItem(), tocSaleImportLog.getId(), request.getRateMap());
        return insert;
    }

    @Override
    public Page<TocSaleImportLogModel> pageQuery(TocSaleImportLogQueryRequest request) {
        SaleChannelRequestManager.fill(request, 0, 1, 2);
        return tocSaleImportLogMapper.pageQuery(PageUtils.buildPage(request), request);
    }

    @Override
    public Integer delete(Long id) {
        TocSaleImportLog tocSaleImportLog = tocSaleImportLogMapper.selectById(id);
        if (!TocSaleImportLogOrderStatusEnum.S1.getIntValue().equals(tocSaleImportLog.getOrderStatus())) {
            throw new BusinessException(OrderRespCode.SALE_ORDER_DETAIL_STATUS_IS_NOT_MET);
        }
        tocSaleImportLogDetailMapper.update(null,
                new LambdaUpdateWrapper<TocSaleImportLogDetail>()
                        .eq(TocSaleImportLogDetail::getLogId, tocSaleImportLog.getId())
                        .set(TocSaleImportLogDetail::getIsDeleted, true)
        );
        return tocSaleImportLogMapper.delById(id);
    }

    @Override
    public TocSaleImportLogQueryDetailResponse queryDetail(Long id) {
        TocSaleImportLogQueryDetailResponse response = new TocSaleImportLogQueryDetailResponse();
        TocSaleImportLog tocSaleImportLog = tocSaleImportLogMapper.selectById(id);
        if (ObjectUtil.isEmpty(tocSaleImportLog)) {
            return response;
        }
        TocSaleImportLogInfo tocSaleImportLogInfo = tocSaleImportLogConverter.convertFrom(tocSaleImportLog);
        response.setMain(tocSaleImportLogInfo);
        List<TocSaleImportLogDetailInfo> item = tocSaleImportLogDetailMapper.selectListByLogId(tocSaleImportLog.getId());
        response.setItem(item);
        return response;
    }

    @Override
    public Integer updateStatus(SaleOrderUpdateStatusRequest request) {
        return tocSaleImportLogMapper.update(null, new LambdaUpdateWrapper<TocSaleImportLog>()
                .eq(TocSaleImportLog::getId, request.getId())
                .set(TocSaleImportLog::getOrderStatus, request.getOrderStatus())
        );
    }

    @Override
    public List<Long> selectNotProcessed() {
        return tocSaleImportLogMapper.selectNotProcessed();
    }

    @Override
    public TocSaleImportLogInfo queryById(Long id) {
        return tocSaleImportLogConverter.convertFrom(tocSaleImportLogMapper.selectById(id));
    }

    @Override
    public List<String> checkShopOrderNoIfExist(CheckShopOrderNoIfExistRequest checkShopOrderNoIfExistRequest) {
        return tocSaleImportLogDetailMapper.checkShopOrderNoIfExist(checkShopOrderNoIfExistRequest);
    }
}
